﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Hello.Models
{
    public class Calculator
    {
        const string CalcBase = "{0} {1} {2} = {3}";
        Dictionary<char, Func<double, double, double>> operationDict = default(Dictionary<char, Func<double, double, double>>);

        public Calculator()
        {
            operationDict = new Dictionary<char, Func<double, double, double>>();
            operationDict.Add('+',(a,b) => a + b);
            operationDict.Add('-', (a, b) => a - b);
            operationDict.Add('*', (a, b) => a * b);
            operationDict.Add('/', (a, b) => a / b);
        }

        public string Handle(double x, char operation, double y)
        {
            string s = (operationDict.ContainsKey(operation)) ?
                string.Format(CalcBase, x, operation, y, operationDict[operation](x, y))
                : "Error!";
            return HttpUtility.HtmlEncode(s);
        }

    }
}